//
//  NetWorking.m
//  KaKaWallet
//
//  Created by admin on 16/5/29.
//  Copyright © 2016年 ibendi. All rights reserved.
//

#import "ZYNetWorking.h"
#import <AFNetworking/AFHTTPSessionManager.h>



@interface ZYNetWorking ()

@property (strong, nonatomic) AFHTTPSessionManager *sessionManager;

@end

@implementation ZYNetWorking

+ (instancetype)shared {
    static ZYNetWorking *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ZYNetWorking alloc] init];
    });
    return manager;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (AFHTTPSessionManager *)sessionManager {
    if (!_sessionManager) {
        _sessionManager = [AFHTTPSessionManager manager];
        
//        _sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
//        _sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        
        _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
//          _sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];

        
//        _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
//                                                                                  @"text/html",
//                                                                                  @"text/json",
//                                                                                  @"text/plain",
//                                                                                  @"text/javascript",
//                                                                                  @"text/xml",
//                                                                                  @"image/*"]];

    }
    return _sessionManager;
}

- (void)cancel {
    [self.sessionManager.operationQueue cancelAllOperations];
}

- (void)getRequest:(nonnull NSString *)urlString parameters:(id)parameters
{
    [self.sessionManager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(progressWithProgress:)]) {
            [self.delegate progressWithProgress:downloadProgress];
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(successWithTask:responseObject:)]) {
            [self.delegate successWithTask:task responseObject:responseObject];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(failureWithTask:error:)]) {
            [self.delegate failureWithTask:task error:error];
        }
    }];
}

- (void)postRequest:(nonnull NSString *)urlString parameters:(id)parameters
{
    //    NSLog(@"%@",parameters);
    [self.sessionManager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(progressWithProgress:)]) {
            [self.delegate progressWithProgress:downloadProgress];
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(successWithTask:responseObject:)]) {
            [self.delegate successWithTask:task responseObject:responseObject];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(failureWithTask:error:)]) {
            [self.delegate failureWithTask:task error:error];
        }
    }];
}

- (void)upData:(NSString *)URLString parameters:(id)parameters fileData:(NSData *)fileData name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType
{
    [self.sessionManager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(progressWithProgress:)]) {
            [self.delegate progressWithProgress:uploadProgress];
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(successWithTask:responseObject:)]) {
            [self.delegate successWithTask:task responseObject:responseObject];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(failureWithTask:error:)]) {
            [self.delegate failureWithTask:task error:error];
        }    }];
}

- (void)upImage:(NSString *)URLString parameters:(id)parameters image:(UIImage *)image   fileName:(NSString *)fileName
{
    [self upData:URLString parameters:parameters fileData:UIImagePNGRepresentation(image) name:@"file" fileName:fileName mimeType:@"image/jpg"];
}


- (void)getRequest:(NSString *)urlString parameters:(id)parameters success:(void(^)(id data))success failure:(void (^)(NSError *error))failure {
    NSLog(@"%@",parameters);
    NSMutableString *string = [[NSMutableString alloc] init];
    [string appendString:urlString];
    [string appendString:@"?"];
    int i = 0;
    for (NSString *key in [parameters allKeys]) {
        if (i == 0) {
            [string appendString:[NSString stringWithFormat:@"%@=%@",key,parameters[key]]];
        }else {
            [string appendString:[NSString stringWithFormat:@"&%@=%@",key,parameters[key]]];
        }
        i++;
    }
    NSLog(@"url = %@",string);
    [self.sessionManager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

- (void)postRequest:(nonnull NSString *)urlString parameters:(id)parameters success:(void(^)(id data))success failure:(void(^)(NSError *error))failure
{
    
    NSLog(@"url = %@",urlString);

    [self.sessionManager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}


- (void)upData:(NSString *)URLString parameters:(id)parameters fileData:(NSData *)fileData name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType success:(void(^)(id data))success failure:(void(^)(NSError *error))failure{
    [self.sessionManager.requestSerializer setTimeoutInterval:30];
    [self.sessionManager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)upImage:(NSString *)URLString parameters:(id)parameters image:(UIImage *)image name:(NSString *)name fileName:(NSString *)fileName success:(void(^)(id data))success failure:(void(^)(NSError *error))failure {
    [self.sessionManager.requestSerializer setTimeoutInterval:30];
    //    [self.sessionManager.responseSerializer setAcceptableContentTypes:[NSSet setWithArray:@[@"text/html",@"application/json"]]];
    [self.sessionManager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:name fileName:fileName mimeType:@"image/jpg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}



@end
